package com.analyse.stock.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.analyse.stock.constant.ApiConstant;
import com.analyse.stock.constant.StockConstant;
import com.analyse.stock.mapper.FundFlowRealTimeMapper;
import com.analyse.stock.model.FundFlowRealTimeModel;
import com.analyse.stock.model.StockInfoModel;
import com.analyse.stock.service.IFundFlowRealTime;
import com.analyse.stock.service.IStockInfo;
import com.analyse.stock.utils.HttpUtil;
import com.analyse.stock.utils.StockStringUtil;
import com.analyse.stock.vo.FinanceDataVO;
import com.analyse.stock.vo.FundFlowDataVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Slf4j
@Service
public class FundFlowRealRealTimeService extends ServiceImpl<FundFlowRealTimeMapper, FundFlowRealTimeModel> implements IFundFlowRealTime {
    @Autowired
    private IStockInfo istockInfo;

    @Override
    public Integer getLatest(String stockCodes){
        Set<String> sets = Sets.newHashSet();
        if(StringUtils.isNotBlank(stockCodes)){
            sets.addAll(Arrays.asList(stockCodes.split(StrUtil.COMMA)));
        }
        List<StockInfoModel> stockList = istockInfo.getStockList(sets);
        if(CollUtil.isEmpty(stockList)){
            return 0;
        }
        AtomicInteger num = new AtomicInteger(0);
        List<List<StockInfoModel>> partList = Lists.partition(stockList,50);
        for(List<StockInfoModel> stList : partList) {
            long time = System.currentTimeMillis()*1001;
            String secids = stList.stream().map(e->String.format("%s.%s",e.getMarkCode(),e.getStockCode())).collect(Collectors.joining(StrUtil.COMMA));
            String url = StrUtil.format(ApiConstant.REAL_TIME_FUND_FLOW_STAT_URL,secids,time);
            String result = HttpUtil.sendGet(url,"application/javascript", StockConstant.UTF8,6000L);
            if(StringUtils.isBlank(result)){
                log.info("## 股票:{}, 没有获取到 最新的资金流向数据-----",secids);
                continue;
            }
            JSONObject object = JSON.parseObject(result.replace("data:",""));
            FundFlowDataVO dataVO = StockStringUtil.getObject(object.getString("data"), FundFlowDataVO.class);
            log.info("## 股票:{}, 开始解析 最新的资金流向数据-----",secids);
            List<FundFlowRealTimeModel> fundFlows = new ArrayList<>();
            for(FinanceDataVO financeDataVO : dataVO.getDiff()){
                FundFlowRealTimeModel model = FundFlowRealTimeModel.transFundFlowTimeModel(financeDataVO);
                fundFlows.add(model);
            }
            super.saveBatch(fundFlows);
            num.getAndAdd(fundFlows.size());
        }
        return num.get();
    }
}
